<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<link rel="stylesheet" type="text/css" href="default.css" />
<title>spacecreeps.com</title>
</head>
<body>

<table><tr><td class="left leftlarge">
<p>
<?php


include("encode.php");
include("creepsearch.php");
include("connect.php");

if (!$_GET['type']) {
   $_GET['type']=Score;
   $_GET['prev']=0;
}
  
//Print a greeting if the form was submitted
if ($_GET['type']) {
	$db = connect();
	print "<br \>";
  //Print what was submitted in the form
	if (!db)
	{
		print "Failed to connect to db.";
		exit;
	}
	else
	{
		db();

		$limit = "50";

		$result = mysql_query("SELECT value FROM Settings WHERE name='curgame'");
		
		$row = mysql_fetch_array($result, MYSQL_NUM);
		$curgame = intval($row[0]);
		
		$result = mysql_query("SELECT id FROM GameNums WHERE gamenum=".$curgame);
		$row = mysql_fetch_array($result, MYSQL_NUM);
		$curday = intval($row[0]);
		
		$query = "SELECT id,username,groupname,gamenumber,score,wavenum,downloads FROM CreepData ";

		if ($_GET['prev']
		and intval($_GET['prev'])<$curday and intval($_GET['prev'])>=0)
		{
		
			// get the id that was backwards "prev" number of days, as long as it isn't trying to go forward or too far back
		
			$result = mysql_query("SELECT gamenum FROM GameNums WHERE id="
			.strval($curday-intval($_GET['prev']))
			);
			$row = mysql_fetch_array($result, MYSQL_NUM);
			
			$query = $query."WHERE gamenumber=".$row[0]." ";
		}
		else
		{
			$query = $query."WHERE gamenumber=".$curgame." ";
		}

		// now look at the specific type
		
		if ($_GET['type']=="Username")
		{
			$query = $query."AND username=\"".str_hex($_GET['query'])."\" ORDER BY score DESC";
		}
		elseif ($_GET['type']=="Groupname")
		{
			$query = $query."AND groupname=\"".str_hex($_GET['query'])."\" ORDER BY score DESC";
		}
		else // default to score ranking
		{
			if (intval($_GET['query']))
			{
				$query = $query."AND score<".$_GET['query']." ";
			}
			$query = $query."ORDER BY score DESC LIMIT 50";
		}

		print "Games from previous days may be replayed by clicking their record #. Today's game is locked to prevent cheating.";

		$result = mysql_query($query);
		
		print '<table class="search"><tr><td>Record #</td><td>Name</td><td>Group</td><td>Game #</td></td><td>Score</td><td>Wave</td></td><td>D/Ls</td></tr>';

		while ($row = mysql_fetch_array($result, MYSQL_NUM))
		{
		print "<tr>";
		if ($row[3]!=$curgame) // id, if the gamenumber is not today's then also allow playback
			printf('<td><a href="playback.php?rec=%s">%s</a></td>',$row[0],$row[0]);
		else
			printf("<td><pre>%s</pre></td>",$row[0]);		
		printf("<td><pre>%s</pre></td>",hex_str($row[1])); // username
		printf("<td><pre>%s</pre></td>",hex_str($row[2])); // groupname
		printf("<td><pre>%s</pre></td>",$row[3]); // gamenumber
		printf("<td><pre>%s</pre></td>",$row[4]); // score
		printf("<td><pre>%s</pre></td>",$row[5]); // wavenum
		printf("<td><pre>%s</pre></td>",$row[6]); // downloads
		print "</tr>";
		}

		print "</table><br \>Got ";
		printf(mysql_num_rows($result));
		print " results.";
		print '<br \><a href="http://spacecreeps.com/creepsearchresult.php?prev='.
		strval(intval($_GET['prev'])+1).'&query='.$_GET['query']."&type=".$_GET['type']
		.'">Yesterday</a>';
		print '&nbsp|&nbsp<a href="http://spacecreeps.com/creepsearchresult.php?prev='.
		strval(intval($_GET['prev'])-1).'&query='.$_GET['query']."&type=".$_GET['type']
		.'">Tomorrow</a>';
}}
?>
<br \><a href="http://spacecreeps.com">Play the
game!</a>
<?php 
include("footer.php");
?>
